package com.lai.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.lai.common.exception.BizCodeEnum;
import com.lai.common.exception.RRException;
import com.lai.common.to.SocialUserDto;
import com.lai.gulimall.member.dto.MemberLoginDto;
import com.lai.gulimall.member.dto.MemberRegisterDto;
import com.lai.gulimall.member.feign.MemberFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import com.lai.gulimall.member.entity.MemberEntity;
import com.lai.gulimall.member.service.MemberService;
import com.lai.common.utils.PageUtils;
import com.lai.common.utils.R;



/**
 * 会员
 *
 * @author laiciqi
 * @email 602469238@qq.com
 * @date 2021-11-07 12:13:51
 */
@RefreshScope
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    MemberFeignService couponFeignService;

    @Value("${member.user.name}")
    private String name;
    @Value("${member.user.age}")
    private Integer age;


    @RequestMapping("/test1")
    public R test1() {
        return R.ok().put("name", name).put("age", age);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @PostMapping("/register")
    public R register(@RequestBody MemberRegisterDto memberRegisterDto) {
        try {
            memberService.register(memberRegisterDto);
        } catch (RRException e) {
            return R.error(e.getCode(), e.getMsg());
        }
        return R.ok();
    }


    /**
     * 本地登录
     *
     * @param memberLoginDto
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody MemberLoginDto memberLoginDto) {
        MemberEntity memberEntity = memberService.login(memberLoginDto);
        if (!StringUtils.isEmpty(memberEntity)) {
            return R.ok().put("data", memberEntity);
        } else {
            return R.error(BizCodeEnum.ACCOUNT_PASSWORD_INVALID_EXCEPTION.getCode(), BizCodeEnum.ACCOUNT_PASSWORD_INVALID_EXCEPTION.getMsg());
        }
    }

    @PostMapping("/oauth2/login")
    public R oauth2Login(@RequestBody SocialUserDto socialUserDto) {
        MemberEntity memberEntity = memberService.oauth2Login(socialUserDto);
        if (memberEntity != null) {
            return R.ok().put("data", memberEntity);
        } else {
            return R.error(BizCodeEnum.ACCOUNT_PASSWORD_INVALID_EXCEPTION.getCode(), BizCodeEnum.ACCOUNT_PASSWORD_INVALID_EXCEPTION.getMsg());
        }
    }



}
